This is an easy-to-access reference page for plotly, an interactive data visualization package in R. Most of these plots are directly copied from the free online book, plotly for R, by Carson Sievert. Possible to use plotly in just R, dont have to connect it to an account online, simply follow the first step in Plot.ly setup. If you want to connect your account online, more tools for analysis, and sharing. Limits apply to free accounts though, like 25 plots in the server maximum. Cookbook section isn’t as inclusive as the online text book, i just kept stuff i think would be useful / interesting to me.


Setup

  1. Install and load plotly package in R
install.packages('plotly')
library('plotly')
  1. Create an online account - https://plot.ly/feed/

  2. Set credentials in R

Sys.setenv("plotly_username"="username-goes-here")
Sys.setenv("plotly_api_key"="api-key-goes-here")
  1. Create a plotly test object
p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
  1. Push test object to plot.ly servers
api_create(p, filename = "midwest-boxplots")



2D Scatterplot

subplot(
  plot_ly(mpg, x = ~cty, y = ~hwy, showlegend=FALSE) %>%
    add_markers(color = ~cyl), 
  plot_ly(mpg, x = ~cty, y = ~hwy) %>%
    add_markers(alpha = 0.6,
                symbol = ~factor(cyl),
                size = ~cyl,
                sizes = c(5,20),
                color = ~factor(cyl)
                )
) 


3D Scatterplot

col1 <- colorRamp(c("red", "blue"))

plot_ly(mpg, x = ~cty, y = ~hwy, z = ~cyl) %>%
  add_markers(color = ~cyl, colors = col1)


Line Plot

col2 <- "Pastel2"
plot_ly(line_ex, x = ~date, y = ~median) %>%
  add_lines(color = ~city, colors = col2, linetype = ~city)


Hex Plot

p <- ggplot(txhousing, aes(date, median)) +
  geom_line(aes(group = city), alpha = 0.2)

subplot(p, 
  ggplot(txhousing, aes(date, median)) + geom_hex(), 
  shareX = TRUE, shareY = TRUE
)


Density Plot

kerns <- c("gaussian", "epanechnikov", "rectangular", 
          "triangular", "biweight", "cosine", "optcosine")
p <- plot_ly()
for (k in kerns) {
  d <- density(txhousing$median, kernel = k, na.rm = TRUE)
  p <- add_lines(p, x = d$x, y = d$y, name = k)
}
layout(p, xaxis = list(title = "Median monthly price"))


Histogram

p1 <- plot_ly(diamonds, x = ~price) %>% add_histogram(name = "plotly.js")

price_hist <- function(method = "FD") {
  h <- hist(diamonds$price, breaks = method, plot = FALSE)
  plot_ly(x = h$mids, y = h$counts) %>% add_bars(name = method)
}

subplot(
  p1, price_hist(), price_hist("Sturges"),  price_hist("Scott"),
  nrows = 4, shareX = TRUE
)


Bar Chart

p1 <- plot_ly(diamonds, x = ~cut) %>% add_histogram()

p2 <- diamonds %>%
  dplyr::count(cut) %>%
  plot_ly(x = ~cut, y = ~n, showlegend=FALSE) %>% 
  add_bars()

subplot(p1, p2) 


Box Plot

d <- diamonds %>%
  mutate(cc = interaction(clarity, cut))

# interaction levels sorted by median price
lvls <- d %>%
  group_by(cc) %>%
  summarise(m = median(price)) %>%
  arrange(m) %>%
  .[["cc"]]

plot_ly(d, x = ~price, y = ~factor(cc, lvls)) %>%
  add_boxplot(color = ~clarity) %>%
  layout(yaxis = list(title = ""), margin = list(l = 100))


Surface Plot

x <- seq_len(nrow(volcano)) + 100
y <- seq_len(ncol(volcano)) + 500
plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~volcano)